/*
 * @Author: WangLi
 * @Date: 2024-03-10 09:13:33
 * @LastEditors: WangLi
 * @LastEditTime: 2024-05-10 19:21:48
 */
import moment from "moment";
import WPage from "../../common/WPage";
import { getUuid, writeBLECharacteristicValue } from "../../utils/common";

WPage({
  data: {
    title: "分享钥匙",
    typeList: [
      { value: "1", text: "普通分享" },
      { value: "2", text: "临时钥匙" },
    ],
    activeType: "1",
    isNormalShare: true,
    authIndex: 0,
    authList: ["普通权限", "特殊权限"],
    account: "",
    name: "",
    auth: "普通权限",
    startDate: moment().format("YYYY-MM-DD"),
    startTime: moment().format("HH:mm"),
    endDate: moment().add(3, "days").format("YYYY-MM-DD"),
    endTime: moment().format("HH:mm"),
  },
  onLoad(options) {
    this.init();
  },
  onShow() {
    this.init();
  },
  init() {},
  onTypeChange(e) {
    this.setData({
      activeType: e.detail.value,
      isNormalShare: e.detail.value === "1",
      startDate: moment().format("YYYY-MM-DD"),
      startTime: moment().format("HH:mm"),
      endDate: moment().add(3, "days").format("YYYY-MM-DD"),
      endTime: moment().format("HH:mm"),
    });
  },
  onAuthChange(e) {
    const authIndex = e.detail.value;
    this.setData({
      authIndex,
      auth: this.data.authList[authIndex],
    });
  },
  onStartDateChange(e) {
    this.setData({
      startDate: e.detail.value,
    });
  },
  onStartTimeChange(e) {
    this.setData({
      startTime: e.detail.value,
    });
  },
  handleSubmit() {
    if (this.data.activeType === "1") {
      this.handleNormalSubmit();
    } else {
      this.handleTempSubmit();
    }
  },
  handleNormalSubmit() {
    const { account, name, auth, startDate, startTime, endDate, endTime } = this.data;
    const serviceId = getUuid("5503");
    writeBLECharacteristicValue(serviceId, getUuid("0030"), account);
    writeBLECharacteristicValue(serviceId, getUuid("0031"), name);
    writeBLECharacteristicValue(serviceId, getUuid("0032"), auth);
    writeBLECharacteristicValue(serviceId, getUuid("0033"), startDate + startTime);
    writeBLECharacteristicValue(serviceId, getUuid("0034"), endDate + endTime);
    writeBLECharacteristicValue(serviceId, getUuid("FF30"), "", "分享钥匙");
  },
  handleTempSubmit() {
    const { startDate, startTime, endDate, endTime } = this.data;
    const serviceId = getUuid("5504");
    writeBLECharacteristicValue(serviceId, getUuid("0040"), startDate + startTime);
    writeBLECharacteristicValue(serviceId, getUuid("0041"), endDate + endTime);
    writeBLECharacteristicValue(serviceId, getUuid("FF40"), "", "分享钥匙");
  },
});
